import requests
import json
import smtplib
from email.mime.text import MIMEText 
from email.header import Header 
import ssl
import schedule
import time

login_data={
    "account": "13652840560",
    "password": "roysxlife"
}

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.81",
    "Cookie": "BCUSS=F1533C6DEA7E72A73927249807255EE7B09DA3B2194C44586ADA7623F4F6D2692913F49F5EE783D488465B6E201AA2FF5B83807EF3598BEA101EC7E65EA5B563EA3E58D491E233540E2AA2AB66FDF3101C577D1F31FB52D02AD5"
}

login_api="https://www.baichuanweb.com/bcuser/user/login"
wait_api="https://www.baichuanweb.com/vpsapi/first/waitlist"

def autoLogin():
    rep=requests.post(login_api,data=login_data)
    cookies=rep.cookies
    for cookie in cookies:
        token=cookie.name+"="+cookie.value
    headers['Cookie']=token

def checkSign():
    quesion_rep=requests.get(wait_api,headers=headers)
    print(quesion_rep)
    flag=quesion_rep.json()['data']['canReceive']
    print("flag="+str(flag))
    if flag==1:
        return "flag="+str(flag)+"目前可领取任务"
    else:
        return "flag="+str(flag)+"目前无任务"

def sendMessage(mes):
    sender = 'roysxlife@163.com' 
    receivers = ['1915878737@qq.com'] # 接收邮件，可设置为你的QQ邮箱或者其他邮箱 
    # 第三方 SMTP 服务 
    mail_host="smtp.163.com" #设置服务器 
    mail_user="roysxlife@163.com" #用户名 
    mail_pass="NOUNWUHTSOBRYDUC" #口令

    if mes=="flag=1目前可领取任务":
        message = MIMEText(mes, 'plain', 'utf-8') 
        message['From'] = Header("百川", 'utf-8') 
        message['To'] = Header("测试", 'utf-8') 

        subject = '百川领取任务标志' 
        message['Subject'] = Header(subject, 'utf-8') 
        try: 
            smtpObj = smtplib.SMTP(mail_host,25)
            smtpObj.starttls()
            print(1)
            login_res = smtpObj.login(mail_user,mail_pass)
            print(login_res)
            if login_res[0] == 235:
                print("login suc")
            else:
                print("login failed") 
            smtpObj.sendmail(sender, receivers, message.as_string()) 
            print("邮件发送成功")
        except smtplib.SMTPException as e: 
            print("Error: 无法发送邮件")
            print(str(e))

def task():   
    sendMessage(checkSign())

if __name__=='__main__':
    autoLogin()
    schedule.every().hour.do(task)

    while True:
        schedule.run_pending()
        time.sleep(1)


